package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.LoanCompensationStatementDao;
import com.fdb.efp.nls.service.domain.LoanCompensationStatement;
import com.fdb.efp.nls.service.facade.LoanCompensationStatementServiceV2;
import com.fdb.efp.nls.service.vo.LoanCompensationStatementVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 
 * 百度代偿清单
 * 
 * @author liyakui
 * @since 2019年1月7日 下午8:11:51
 * @version 1.0
 */
@Service("loanCompensationStatementServiceV2")
public class LoanCompensationStatementServiceV2Impl implements LoanCompensationStatementServiceV2, FrameworkService {
	Logger logger = LoggerFactory.getLogger(LoanCompensationStatementServiceV2Impl.class);
	@Autowired
	private LoanCompensationStatementDao loanCompensationStatementDao;

	/**
	 * 
	 * 批量插入数据
	 * 
	 * @param loanCompensationStatementVos
	 * @return
	 * @author liyakui
	 * @since 2019年1月9日 下午4:43:55
	 * @version 1.0
	 */
	@Override
	public int bathInsert(List<LoanCompensationStatementVo> loanCompensationStatementVos) throws Exception {
		logger.info("批量插入数据开始！");
		int count = 0;
		try {
			@SuppressWarnings("unchecked")
			List<LoanCompensationStatement> loanCompensationStatements = (List<LoanCompensationStatement>) beansCopy(
					loanCompensationStatementVos, LoanCompensationStatement.class);
			count = loanCompensationStatementDao.bathInsert(loanCompensationStatements);
			logger.info("批量插入数据成功！插入条数：" + count);
		} catch (DuplicateKeyException e1) {
			logger.debug("借据号重复！" + e1.getMessage());
			throw new Exception("借据号重复！");
		} catch (Exception e) {
			count = -1;
			logger.debug("批量插入数据异常！" + e.getMessage());
			throw new Exception("批量插入数据异常！");
		}

		return count;
	}

	@Override
	public int insertOrUpdateBath(List<LoanCompensationStatementVo> loanCompensationStatementVos) throws Exception {
		logger.info("批量插入数据或更新开始！");
		try {
			List<LoanCompensationStatement> loanCompensationStatements = (List<LoanCompensationStatement>) beansCopy(
					loanCompensationStatementVos, LoanCompensationStatement.class);
			return loanCompensationStatementDao.bathInsertOrUpdate(loanCompensationStatements);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("失败：",e);
		}
		return -1;
	}

	/**
	 * 
	 * 根据日期查询数据
	 * 
	 * @param loanCompensationStatementVo
	 * @return
	 * @author liyakui
	 * @since 2019年1月21日 下午3:56:52
	 * @version 1.0
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<LoanCompensationStatementVo> queryLoanCompensationStatementByDate(LoanCompensationStatementVo loanCompensationStatementVo)
			throws Exception {
		logger.info("查询数据开始！参数为：" + loanCompensationStatementVo);
		List<LoanCompensationStatementVo> list = null;
		try {
			LoanCompensationStatement loanCompensationStatement = new LoanCompensationStatement();
			beanCopy(loanCompensationStatementVo, loanCompensationStatement);
			List<LoanCompensationStatement> loanCompensationStatements = loanCompensationStatementDao
					.queryLoanCompensationStatementByDate(loanCompensationStatement);
			list = (List<LoanCompensationStatementVo>) beansCopy(loanCompensationStatements, LoanCompensationStatementVo.class);
			logger.info("查询数据成功！结果为：" + list);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("查询数据异常！" + e.getMessage());
			throw e;
		}
		return list;
	}

	/**
	 * 
	 * 批量更新代偿状态
	 * 
	 * @param loanCompensationStatementVo
	 * @param listStr
	 * @return
	 * @author liyakui
	 * @since 2019年1月21日 下午4:17:38
	 * @version 1.0
	 */
	@Override
	public int bathUpdateLoanCompensationStatement(List<LoanCompensationStatementVo> loanCompensationStatementVos) throws Exception {
		logger.info("批量更新代偿状态开始！参数：" + loanCompensationStatementVos);
		int count = 0;
		try {
			@SuppressWarnings("unchecked")
			List<LoanCompensationStatement> loanCompensationStatements = (List<LoanCompensationStatement>) beansCopy(
					loanCompensationStatementVos, LoanCompensationStatement.class);
			count = loanCompensationStatementDao.bathUpdateLoanCompensationStatement(loanCompensationStatements);
			logger.info("批量更新成功！更新条数：" + count);
		} catch (Exception e) {
			logger.debug("批量更新代偿状态异常！" + e.getMessage());
			throw e;
		}
		return count;
	}

	/**
	 * 
	 * 查询金额汇总
	 * 
	 * @param loanCompensationStatementVo
	 * @return
	 * @throws Exception
	 * @author liyakui
	 * @since 2019年1月24日 上午10:12:57
	 * @version 1.0
	 */
	@Override
	public Map<String, BigDecimal> querySumBal(LoanCompensationStatementVo loanCompensationStatementVo) throws Exception {
		logger.info("查询金额汇总开始！参数：" + loanCompensationStatementVo);
		Map<String, BigDecimal> result = null;
		try {
			LoanCompensationStatement loanCompensationStatement = new LoanCompensationStatement();
			beanCopy(loanCompensationStatementVo, loanCompensationStatement);
			result = loanCompensationStatementDao.querySumBal(loanCompensationStatement);
			logger.debug("查询成功！返回结果：" + result);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("查询数据异常！" + e.getMessage());
		}

		return result;
	}
	/**
	 * 通过对账状态和日期统计对账明细数目: <br>
	 *
	 * 0.1:caoxin:2019/4/4 9:46: TODO: <br>
	 * @author caoxin
	 * @return
	 * @since 2019/4/4 9:46
	 * @date 2019/4/4 9:46
	 * @version 0.1
	 */
	@Override
	public int countLoanCompensationStatementByCheckState(LoanCompensationStatementVo loanCompensationStatementVO) {
		logger.info("通过对账状态和某个日期的对账明细：" + "日期:"+loanCompensationStatementVO.getCheckSts()+ "对账状态："+loanCompensationStatementVO.getCreateTime());
		int result = 0;
		try {
			LoanCompensationStatement loanCompensationStatement = new LoanCompensationStatement();
			beanCopy(loanCompensationStatementVO, loanCompensationStatement);
			result = loanCompensationStatementDao.countLoanCompensationStatementByCheckState(loanCompensationStatement);
			logger.debug("查询成功！返回结果：" + result);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("查询数据异常！" + e.getMessage());
		}
		return result;
	}



	@Override
	public int insert(LoanCompensationStatementVo loanCompensationStatementVo) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insertSelective(LoanCompensationStatementVo loanCompensationStatementVo) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * 
	 * TODO 根据日期修改对账状态<br>
	 * 0.1:wanglihua:2019年4月15日 上午11:21:27:TODO <br>
	 * @author wanglihua
	 * @since 2019年4月15日 上午11:21:27
	 * @date 2019年4月15日 上午11:21:27
	 * @version 0.1 初始为0.1,后续变更则修改
	 */
//	@Override
//	public int timeUpdateLoanCompensationStatement(String curDate) {
//		logger.info("修改对账状态开始！参数：" + curDate);
//		int flag = 0;
//		try {
////			LoanCompensationStatement loanCompensationStatement = new LoanCompensationStatement();
////			beanCopy(curDate, loanCompensationStatement);
//			flag = loanCompensationStatementDao.timeUpdateLoanCompensationStatement(curDate);
//			logger.info("批量更新成功！" );
//		} catch (Exception e) {
//			logger.debug("修改对账状态异常！" + e.getMessage());
//			throw e;
//		}
//		return flag;
//	}

	@Override
	public int timeUpdateLoanCompensationStatement(String curDate, String successFlag) {
		logger.info("修改状态开始！参数：" + curDate + "," + successFlag);
		int flag = 0;
		try {
//			LoanCompensationStatement loanCompensationStatement = new LoanCompensationStatement();
//			beanCopy(curDate, loanCompensationStatement);
			flag = loanCompensationStatementDao.timeUpdateLoanCompensationStatement(curDate,successFlag);
			logger.info("批量更新成功！" );
		} catch (Exception e) {
			logger.debug("修改状态异常！" + e.getMessage());
			flag = -1;
			throw e;
		}
		return flag;
	}

	/**
	 * 批量通过借据号更新对账明细: <br>
	 *
	 * 0.1:caoxin:2019/4/8 19:59: TODO: <br>
	 * @author caoxin
	 * @return
	 * @since 2019/4/8 19:59
	 * @date 2019/4/8 19:59
	 * @version 0.1
	 */
	@Override
	public int updateBatchByBillNo(List<LoanCompensationStatementVo> loanCompensationStatementVos) {
		logger.info("批量更新对账明细开始...");
		int result = 0;
		try {
			List<LoanCompensationStatement> loanCompensationStatements = (List<LoanCompensationStatement>) beansCopy(
					loanCompensationStatementVos, LoanCompensationStatement.class);
			result = loanCompensationStatementDao.updateBatchByBillNo(loanCompensationStatements);
			logger.debug("批量更新成功！返回结果：" + result);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("查询数据异常！" + e.getMessage());
		}
		return result;
	}

	/**
	 * 通过借据号更新对账明细: <br>
	 *
	 * 0.1:caoxin:2019/4/10 10:24: TODO: <br>
	 * @author caoxin
	 * @return
	 * @since 2019/4/10 10:24
	 * @date 2019/4/10 10:24
	 * @version 0.1
	 */

	@Override
	public int updateByBillNo(LoanCompensationStatementVo loanCompensationStatementVo) {
		logger.info("更新对账明细开始...");
		int result = 0;
		try {
			LoanCompensationStatement loanCompensationStatement = new LoanCompensationStatement();
			beanCopy(loanCompensationStatementVo, loanCompensationStatement);
			result = loanCompensationStatementDao.updateByBillNo(loanCompensationStatement);
			logger.debug("更新对账文件成功！返回结果：" + result);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("查询数据异常！" + e.getMessage());
		}
		return result;
	}
}
